package superminer.xinfang.com.ipreport.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import superminer.xinfang.com.ipreport.R;
import superminer.xinfang.com.ipreport.bean.VersionBean;
import superminer.xinfang.com.ipreport.network.HttpUtils;
import superminer.xinfang.com.ipreport.network.NetHelper;
import superminer.xinfang.com.ipreport.util.MyGson;
import superminer.xinfang.com.ipreport.util.ToastUtil;
import superminer.xinfang.com.ipreport.util.UpdateManager;
import superminer.xinfang.com.ipreport.util.VerUtils;
import superminer.xinfang.com.ipreport.view.dialog.LoadingDialog;
import superminer.xinfang.com.ipreport.view.dialog.UpdateDialog;

/**
 * 设置
 */
public class SettingActivity extends AppCompatActivity {

    @BindView(R.id.tv_varname)
    TextView tv_varname;   //版本号

    private LoadingDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        dialog=new LoadingDialog(this,0);
        tv_varname.setText("V" + VerUtils.getVerName(this));
    }
    @OnClick({R.id.rl, R.id.ll_update})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl://返回
                finish();
                break;
            case R.id.ll_update://版本检测
                //版本更新
                getVersionInfo();

                break;


        }
    }

    private void getVersionInfo() {
        NetHelper.GetVersionInfo(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                dialog.dismiss();
                VersionBean baseBean = MyGson.getInstance().fromJson(data, VersionBean.class);
                if (baseBean.getType()==1)
                {

                    int verCode = getVerCode(SettingActivity.this);
                    if (verCode<Integer.valueOf(baseBean.getData().getVersionCode()))
                    {
                        //跟新
                        //这里来检测版本是否需要更新
                        UpdateDialog updateDialog = new UpdateDialog(SettingActivity.this, R.style.dialog, baseBean.getData().getUpdateInfo(), baseBean.getData().getUpdateUrl());
                        updateDialog.show();
                    }else
                    {
                        ToastUtil.showToast(SettingActivity.this,"当前是最新版本，不需要更新");
                    }
                }

            }

            @Override
            public void onError(String msg) {
                dialog.dismiss();
                super.onError(msg);
                ToastUtil.showToast(getApplicationContext(), msg);
            }
        });

    }
    /**
     * 获取软件版本号
     * @param context
     * @return
     */
    public static int getVerCode(Context context) {
        int verCode = -1;
        try {
            //注意："com.example.try_downloadfile_progress"对应AndroidManifest.xml里的package="……"部分
            verCode = context.getPackageManager().getPackageInfo(
                    "superminer.xinfang.com.ipreport", 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {

        }
        return verCode;
    }
}
